package com.qq.demo.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qq.demo.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private List<View> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            TextView textView = new TextView(this);
            textView.setText("page" + i);
            mList.add(textView);
        }

        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mViewPager.setAdapter(new MyAdapter(mList));
        //mViewPager.setCurrentItem(0);
        mTabLayout.setupWithViewPager(mViewPager);
        //mViewPager.setOffscreenPageLimit(1);

        //给ViewPager添加监听
        //mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener
        // (mTabLayout));

        //  mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener
        // (mViewPager));


       /*mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("viewpager----------------" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/

      /* mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener
                (mViewPager) {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                System.out.println("---------" + tab.getPosition());
            }

        });*/

       /* try {
            Field mPageChangeListener = TabLayout.class.getDeclaredField("mPageChangeListener");
            mPageChangeListener.setAccessible(true);
            mPageChangeListener.onPageSelected(0);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }*/


       /* mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener
       (mTabLayout){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                System.out.println("viewpager-tab"+position);
            }
        });*/
        MyOnTabSelectedListener listener = new MyOnTabSelectedListener();
        mTabLayout.setOnTabSelectedListener(listener);
        listener.onTabSelected(mTabLayout.getTabAt(0));


    }


    class MyAdapter extends PagerAdapter {

        String[] title = {"viewpager1", "viewpager2", "viewpager3", "viewpager4"};

        List<View> list;

        public MyAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }

    class MyOnTabSelectedListener implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            System.out.println("tab"+tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }


}
